package cn.cjxy.springmvcdemo2.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器   用来处理请求，可以在请求之前之后追加操作或者校验
 * @author huangjie
 * @date 2025/10/13
 */
public class MyInterceptor implements HandlerInterceptor {

    //preHandle()方法在业务处理器处理请求之前被调用   最常用
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        if(request.getParameter("username")!=null&&request.getParameter("password")!=null){
            return true;
        }
        System.out.println("进入拦截器了....");
        //这里可以判断用户是否登录
        //没有登录可以使用 request/response跳转回登录页面

        //注：如果不继续执行返回false,否则返回true
        return  false;
    }

    // postHandle()方法在业务处理器处理请求之后被调用
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        System.out.println("业务处理器处理请求完毕-----");
    }

    // afterCompletion()方法在DispatcherServlet完全处理完请求后被调用
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("DispatcherServlet完全处理完请求-----");
    }
}
